package com.chuangke.context.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chuangke.admin.entity.SysDept;
import com.chuangke.admin.entity.SysRole;
import com.chuangke.admin.entity.SysUser;
import com.chuangke.admin.entity.SysUserAuditDept;
import com.chuangke.admin.entity.SysUserDataDept;
import com.chuangke.admin.service.SysDeptService;
import com.chuangke.admin.service.SysUserAuditDeptService;
import com.chuangke.admin.service.SysUserDataDeptService;
import com.chuangke.admin.service.SysUserRoleQueryService;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.common.constant.SysDeptConstants;
import com.chuangke.common.utils.SecurityUtils;

import cn.hutool.core.util.StrUtil;

@Component
public class ContextService {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleQueryService sysUserRoleQueryService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysUserDataDeptService sysUserDataDeptService;
	@Autowired
	private SysUserAuditDeptService SysUserAuditDeptService;
	
	
	public SysUser getCurrentUser() {
		String userId = SecurityUtils.getUserId();
		return sysUserService.findById(userId);
	}
	
	
	/**
	 * 获取用户的所属组织机构
	 * 
	 * @return
	 */
	public List<SysRole> getCurrentRoles() {
		String userId = SecurityUtils.getUserId();
		if (StrUtil.isBlank(userId)) {
			return null;
		}

		SysUser user = sysUserService.findById(userId);
		if (user == null) {
			return null;
		}
		
		return sysUserRoleQueryService.findRoleByUserId(userId) ;
	}

	public List<String> getCurrentRoleIds() {
		List<SysRole> roleList = getCurrentRoles();
		List<String> roleIdList = roleList.stream().map(SysRole::getName).collect(Collectors.toList());
		return roleIdList;
	}

	/**
	 * 获取当前用户的所属单位id
	 * @return
	 */
	public String getCurrentCompId() {
		String deptId = getCurrentDeptId();
		SysDept parentDept = sysDeptService.findParentByDeptType(deptId, SysDeptConstants.RG_DEPT_TYPE_D);
        return parentDept == null ? deptId : parentDept.getId();
	}

	/**
	 * 获取用户的所属组织机构
	 * 
	 * @return
	 */
	public String getCurrentDeptId() {
		String userId = SecurityUtils.getUserId();
		if (StrUtil.isBlank(userId)) {
			return null;
		}

		SysUser user = sysUserService.findById(userId);
		if (user == null) {
			return null;
		}

		return user.getDeptId();
	}

    /*-------------------获取当前用户的单位数据权限------------------------*/
	
	/**
	 * 获取当前用户的单位数据权限
	 * 
	 * @return
	 */
	public List<SysDept> findUserDataDeptDetailData() {
		List<SysUserDataDept> userDataDeptList = sysUserDataDeptService.find(SecurityUtils.getUserId());

		List<String> deptIdList = userDataDeptList.stream().map(SysUserDataDept::getDeptId)
				.collect(Collectors.toList());

		return getDeptByDeptId(deptIdList);
	}

	/**
	 * 获取用户数据权限的组织机构
	 * 
	 * @return
	 */
	public List<String> findUserDataDeptIdDetailData() {
		return findUserDataDeptIdDetailData(null);
	}
	
	/**
	 * 获取用户数据权限的组织机构
	 * 
	 * @param requestDeptList
	 * @return
	 */
	public List<String> findUserDataDeptIdDetailData(List<String> requestDeptList) {
		List<SysUserDataDept> accessDeptList = sysUserDataDeptService.find(SecurityUtils.getUserId());
		List<String> deptList = accessDeptList == null ? new ArrayList<>()
				: accessDeptList.stream().map(SysUserDataDept::getDeptId).collect(Collectors.toList());
		if (requestDeptList != null && !requestDeptList.isEmpty()) {
			deptList.retainAll(requestDeptList);
		}
		return deptList;
	}

    /*-------------------获取当前用户的单位数据权限和审核权限------------------------*/

	/**
	 * 获取当前用户数据权限和审核权限
	 * @return
	 */
	public List<String> getUserDataAndAuditDeptIdDetail() {
		List<SysUserAuditDept> list = SysUserAuditDeptService.findByUserRole(SecurityUtils.getUserId());
		List<SysUserDataDept> userDataDeptList = sysUserDataDeptService.find(SecurityUtils.getUserId());
		Set<String> deptIdList = new HashSet<>();
		List<String> auditDeptIdList = list.stream().map(SysUserAuditDept::getDeptId).collect(Collectors.toList());
		List<String> editDeptIdList = userDataDeptList.stream().map(SysUserDataDept::getDeptId)
				.collect(Collectors.toList());
		deptIdList.addAll(editDeptIdList);
		deptIdList.addAll(auditDeptIdList);
		return new ArrayList<>(deptIdList);
	}
	
	/**
	 * 获取当前用户数据权限和审核权限
	 * 
	 * @return 带树的结果集
	 */
	public List<SysDept> findUserDataAndAuditDeptTreeData() {
		List<String> deptIdList = getUserDataAndAuditDeptIdDetail();
		return sysDeptService.findKinshipTree(deptIdList);
	}

	/**
	 * 获取当前用户数据权限和审核权限
	 * 
	 * @return
	 */
	public List<SysDept> findUserDataAndAuditDeptDetailData() {
		List<String> deptIdList = getUserDataAndAuditDeptIdDetail();
		return getDeptByDeptId(deptIdList);
	}
	
	/**
	 * 获取当前用户数据权限和审核权限,过滤单位类型
	 * 
	 * @return
	 */
	public List<SysDept> findUserDataAndAuditDeptDetailData(String deptType) {
		List<SysDept> deptList = findUserDataAndAuditDeptDetailData();

		deptList = deptList.stream().filter(dept -> deptType.equals(dept.getDeptType())).collect(Collectors.toList());

		return deptList;
	}

	public List<SysDept> findUserDataAndAuditDeptDetailData(String deptType,String strc) {
		List<SysDept> deptList = findUserDataAndAuditDeptDetailData();

		deptList = deptList.stream().filter(dept -> deptType.equals(dept.getDeptType())&&dept.getStrc()!=null&& dept.getStrc().contains(strc)).collect(Collectors.toList());

		return deptList;
	}

	public List<SysDept> findUserDataAndAuditDeptDetailData(String strc,String stra,String strb) {
		List<SysDept> deptList = findUserDataAndAuditDeptDetailData();

		deptList = deptList.stream().filter(dept -> dept.getStrc()!=null&& dept.getStrc().contains(strc)).collect(Collectors.toList());

		return deptList;
	}
	/**---------------------------------private 方法----------------------------*/

	private List<SysDept> getDeptByDeptId(List<String> deptIdList) {
		List<SysDept> list = new ArrayList<>();
		
		if (CollectionUtils.isEmpty(deptIdList)) {
			return list;
		}
		
		Map<String, SysDept> deptMap = sysDeptService.findMap();
		deptIdList.forEach(deptId -> {
			SysDept ud = deptMap.get(deptId);
			if (ud != null) {
				list.add(ud);
			}
		});

		list.sort(Comparator.comparing(SysDept::getId));

		return list;
	}
}
